package com.tensquare.search.controller;
/*
 *ClassName:ArticleSearchController
 *Description:文章搜索的控制器类，将文章保存到索引库，而不是数据库中
 *@author:可爱的大鸭子
 */

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleSearchService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleSearchController {
    @Autowired
    private ArticleSearchService articleSearchService;

    @RequestMapping(method= RequestMethod.POST)
    public Result save(@RequestBody Article article){
        articleSearchService.save(article);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    /**
     * 根据关键词检索文章索引+分页
     */
    @RequestMapping(value="/search/{page}/{size}",method= RequestMethod.PUT)
    public Result findByTitleLike( @RequestBody Article article,@PathVariable int page, @PathVariable int size){
        String keywords=article.getTitle();
        Page<Article> articlePage = articleSearchService.findByTitleLike(keywords,page,size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult<Article>(articlePage.getTotalElements(), articlePage.getContent()));
    }
}